package com.lier.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.lier.common.exception.EmailExistException;
import com.lier.common.exception.UsernameExistException;
import com.lier.common.valid.ExceptionTypeEnum;
import com.lier.common.vo.GiteeTokenVo;
import com.lier.common.vo.MemberLoginVo;
import com.lier.common.vo.MemberRegisterVo;
import com.lier.gulimall.member.feign.CouponFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lier.gulimall.member.entity.MemberEntity;
import com.lier.gulimall.member.service.MemberService;
import com.lier.common.utils.PageUtils;
import com.lier.common.utils.R;



/**
 * 会员
 *
 * @author lier
 * @email 664478853@qq.com
 * @date 2025-06-06 16:22:20
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;


    @GetMapping("/coupon")
    public R coupon(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("lier");

        R memberCoupons = couponFeignService.getMemberCoupons();
        return R.ok().put("member", memberEntity).put("coupons", memberCoupons.get("coupons"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }

    @PostMapping("gitee/oauth2/login")
    public R giteeLogin(@RequestBody GiteeTokenVo giteeTokenVo){
        try {
            MemberEntity entity = memberService.giteeLogin(giteeTokenVo);
            return R.ok().setData(entity);
        } catch (Exception ex) {
            return R.error(ex.getMessage());
        }
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVo MemberRegisterVo){

        try {
            memberService.register(MemberRegisterVo);
        }catch (UsernameExistException e){
            return R.error(ExceptionTypeEnum.USER_NAME_EXIST.getCode(),
                    ExceptionTypeEnum.USER_NAME_EXIST.getMessage());
        }catch (EmailExistException e){
            return R.error(ExceptionTypeEnum.USER_EMAIL_EXIST.getCode(),
                    ExceptionTypeEnum.USER_EMAIL_EXIST.getMessage());
        }

        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo){

        MemberEntity login = memberService.login(memberLoginVo);
        if(login == null){
            return R.error(ExceptionTypeEnum.ACCOUNT_PASS_ERR_EXCEPTION.getCode(),
                    ExceptionTypeEnum.ACCOUNT_PASS_ERR_EXCEPTION.getMessage());
        }

        return R.ok().setData(login);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
